﻿Public Class Frm_Adver
    Dim bol As Boolean = True
    Dim typeEnemi As String = ""
    Dim avo As Enemie
    Private Sub Btn_parc_ville_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_parc_ville.Click
        Me.Enabled = False
        Frm_Avoc_Bure_ville.txt_ville.Select()
        Frm_Avoc_Bure_ville.Show()
    End Sub

    Private Sub Btn_ajou_tele_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_ajou_tele.Click

        Partager.typePhone = "Tel"
        Partager.isTelClicked = True
        '------
        If List_tous.SelectedIndices.Count > 0 Then
            Try
                Frm_Tele_Bure.List_tele.Items.Clear()
                Partager.list_hatif.Clear()
                For Each numhone As Phone In avo.list_phone
                    If numhone.Type = "Tel" Then
                        Frm_Tele_Bure.List_tele.Items.Add(New ListViewItem(New String() {numhone.Num}))
                    End If
                Next
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try

        End If
        Me.Enabled = False
        Frm_Tele_Bure.txt_tele.Select()
        Frm_Tele_Bure.Show()
    End Sub

    Private Sub Btn_ajou_tele_tabit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_ajou_tele_tabit.Click
        Partager.typePhone = "Fix"
        Partager.isFixClicked = True
        If List_tous.SelectedIndices.Count > 0 Then
            Try
                Frm_Tele_Bure.List_tele.Items.Clear()
                Partager.list_fix.Clear()
                For Each numhone As Phone In avo.list_phone
                    If numhone.Type = "Fix" Then
                        Frm_Tele_Bure.List_tele.Items.Add(New ListViewItem(New String() {numhone.Num}))
                    End If
                Next
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
        End If
        Me.Enabled = False
        Frm_Tele_Bure.txt_tele.Select()
        Frm_Tele_Bure.Show()
    End Sub

    Private Sub Btn_ajou_fax_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_ajou_fax.Click
        Me.Enabled = False
        Partager.isFaxClicked = True
        Frm_Fax_Bure.txt_tele.Select()
        Frm_Fax_Bure.Show()

        If List_tous.SelectedIndices.Count > 0 Then
            Try
                Frm_Fax_Bure.List_tele.Items.Clear()
                Partager.list_fax.Clear()
                For Each numhone As Phone In avo.list_phone
                    If numhone.Type = "Fax" Then
                        Frm_Fax_Bure.List_tele.Items.Add(New ListViewItem(New String() {numhone.Num}))
                    End If
                Next
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try

        End If
    End Sub

    Private Sub Btn_rech_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_rech.Click
        Me.Enabled = False
        Frm_Rech_Adver.Show()
        Partager.type_rech_adver = "Frm_Adver"
    End Sub

    Private Sub Frm_Adver_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        txt_3a2ili.Select()
   Try
            Partager.moi.remplir_List_enemi()
            List_tous.Items.Clear()
            For Each khib As Enemie In Partager.moi.List_enemi
                List_tous.Items.Add(New ListViewItem(New String() {khib.Type, khib.Nom, khib.Prenom, khib.Nom_ent, khib.Representant_legal, khib.Ville, khib.Adress, khib.Email}))
            Next
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        Partager.com_ville(comb_ville, "Comb_Ville", "Ville")
    End Sub

    Private Sub Btn_ajout_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_ajout.Click
        Try

            Partager.close()
            If (List_tous.SelectedIndices.Count > 0) Then
                MsgBox("عفوا لا يمكنك الإضافة، لإضافة خصم جديد عليك الضغط على الزر جديد ثم إدخال البيانات", MsgBoxStyle.Critical, "خطأ في الإضافة")
            Else
                If txt_mail.Text <> "" Then
                    bol = Partager.regEmail(txt_mail.Text)
                    If (bol = False) Then
                        txt_mail.Text = "البريد الإلكتروني خاطئ"
                        txt_mail.ForeColor = Color.Red
                        txt_mail.TextAlign = HorizontalAlignment.Right
                    End If
                End If


                For Each av As Enemie In Partager.moi.List_enemi
                    If txt_cin.Text <> "" And av.Cin = txt_cin.Text Then
                        MsgBox("رقم البطاقة استعمل من قبل", MsgBoxStyle.Critical, "خطأ")
                        Exit Sub
                    End If
                Next

                Dim pourTypeClt As Boolean = True
                If (Radio_dati.Checked) Then
                    If (txt_3a2ili.Text = "" And txt_chakhssi.Text = "") Then
                        pourTypeClt = False
                    End If
                Else
                    If (txt_nom_entrep.Text = "" And txt_momati9anouni.Text = "") Then
                        pourTypeClt = False
                    End If
                End If

                If (pourTypeClt And bol) Then
                    If MsgBox("هل تريد فعلا إضافة هذا الخصم ؟", MsgBoxStyle.YesNo Or MsgBoxStyle.Information, "إضافة خصم") = MsgBoxResult.Yes Then
                        Try
                            Dim cli As New Enemie(1, txt_3a2ili.Text, txt_chakhssi.Text, txt_cin.Text, txt_nom_entrep.Text, txt_momati9anouni.Text, txt_adress_momati9anouni.Text, txt_adresse.Text, txt_mail.Text, comb_ville.Text, typeEnemi, txt_molahadat.Text)
                            Partager.moi.Ajouter_enemi(cli)
                            'l'objet en cours
                            Dim lastItem As Integer = Partager.moi.List_enemi.Count - 1
                            cli = CType(Partager.moi.List_enemi.Item(lastItem), Enemie)
                            For Each d As Phone In Partager.list_hatif
                                cli.Ajouter_phone(d)
                            Next
                            For Each d As Phone In Partager.list_fix
                                cli.Ajouter_phone(d)
                            Next
                            For Each d As Phone In Partager.list_fax
                                cli.Ajouter_phone(d)
                            Next
                            List_tous.Items.Clear()
                            For Each khib As Enemie In Partager.moi.List_enemi
                                List_tous.Items.Add(New ListViewItem(New String() {khib.Type, khib.Nom, khib.Prenom, khib.Nom_ent, khib.Representant_legal, khib.Ville, khib.Adress, khib.Email}))
                            Next
                            MsgBox("تمت إضافة الخصم بنجاح", MsgBoxStyle.Information, "نجاح")
                            Partager.vider(Me)
                        Catch ex As Exception
                            MsgBox(ex.Message, MsgBoxStyle.Exclamation)
                            Partager.close()
                        End Try
                    End If
                Else
                    MsgBox("أدخل البيانات من فضلك", MsgBoxStyle.Critical, "! خـطأ")
                End If
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub Btn_modif_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_modif.Click

        '------------------
        If List_tous.SelectedIndices.Count > 0 Then
            If txt_mail.Text <> "" Then
                bol = Partager.regEmail(txt_mail.Text)
                If (bol = False) Then
                    txt_mail.Text = "البريد الإلكتروني خاطئ"
                    txt_mail.ForeColor = Color.Red
                    txt_mail.TextAlign = HorizontalAlignment.Right
                End If
            End If

            For Each av As Enemie In Partager.moi.List_enemi
                If avo.Id <> av.Id Then
                    If txt_cin.Text <> "" And av.Cin = txt_cin.Text Then
                        MsgBox("رقم البطاقة استعمل من قبل", MsgBoxStyle.Critical, "خطأ")
                        Exit Sub
                    End If

                End If
            Next

            Dim pourTypeClt As Boolean = True
            If (Radio_dati.Checked) Then
                If (txt_3a2ili.Text = "" And txt_chakhssi.Text = "") Then
                    pourTypeClt = False
                End If
            Else
                If (txt_nom_entrep.Text = "" And txt_momati9anouni.Text = "") Then
                    pourTypeClt = False
                End If
            End If


            If (pourTypeClt And bol) Then
                If MsgBox("هل تريد فعلا تعديل هذا الخصم ؟", MsgBoxStyle.YesNo Or MsgBoxStyle.Exclamation, "تعديل خصم") = MsgBoxResult.Yes Then
                    Try
                        Partager.ControleNull(Me)
                        Dim cli As New Enemie(1, txt_3a2ili.Text, txt_chakhssi.Text, txt_cin.Text, txt_nom_entrep.Text, txt_momati9anouni.Text, txt_adress_momati9anouni.Text, txt_adresse.Text, txt_mail.Text, comb_ville.Text, typeEnemi, txt_molahadat.Text)

                        Partager.moi.modifier_enemi(cli, avo.Id)
                        cli.Id = avo.Id
                        If Partager.list_hatif.Count > 0 Or Partager.isTelClicked Then
                            cli.suprimmer_phone(avo.Id, "Tel")
                            For Each d As Phone In Partager.list_hatif
                                cli.Ajouter_phone(d)
                            Next
                        End If
                        If Partager.list_hatif.Count > 0 Or Partager.isFixClicked Then
                            cli.suprimmer_phone(avo.Id, "Fix")
                            For Each d As Phone In Partager.list_fix
                                cli.Ajouter_phone(d)
                            Next
                        End If

                        If Partager.list_hatif.Count > 0 Or Partager.isFaxClicked Then
                            cli.suprimmer_phone(avo.Id, "Fax")
                            For Each d As Phone In Partager.list_fax
                                cli.Ajouter_phone(d)
                            Next
                        End If
                        List_tous.Items.Clear()
                        For Each khib As Enemie In Partager.moi.List_enemi
                            List_tous.Items.Add(New ListViewItem(New String() {khib.Type, khib.Nom, khib.Prenom, khib.Nom_ent, khib.Representant_legal, khib.Ville, khib.Adress, khib.Email}))
                        Next
                        MsgBox("عملية التعديل تمت بنجاح", MsgBoxStyle.Information, "نجاح")
                        Partager.vider(Me)
                    Catch ex As Exception
                        MsgBox(ex.Message, MsgBoxStyle.Exclamation)
                        Partager.close()
                    End Try
                End If
            Else
                MsgBox("أدخل البيانات من فضلك", MsgBoxStyle.Critical, "! خـطأ")
            End If
        Else
            MsgBox("اختر خصم من القائمة", MsgBoxStyle.Information, "خطأ في التعديل")
        End If
    End Sub

    Private Sub Btn_supp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_supp.Click

        Partager.close()
        If List_tous.SelectedIndices.Count > 0 Then
            If MsgBox("هل تريد فعلا حذف هذا الخصم ؟", MsgBoxStyle.YesNo Or MsgBoxStyle.Information, "حذف خصم") = MsgBoxResult.Yes Then
                Try
                    Dim av As Enemie = Partager.moi.List_enemi(List_tous.SelectedIndices.Item(0))
                    Partager.moi.suprimmer_enemi(av.Id)
                    List_tous.Items.Clear()
                    For Each khib As Enemie In Partager.moi.List_enemi
                        List_tous.Items.Add(New ListViewItem(New String() {khib.Type, khib.Nom, khib.Prenom, khib.Nom_ent, khib.Representant_legal, khib.Ville, khib.Adress, khib.Email}))
                    Next
                    Partager.vider(Me)
                    MsgBox("عملية الحذف تمت بنجاح", MsgBoxStyle.Information, "نجاح")
                Catch ex As Exception
                    MsgBox(ex.Message)
                End Try
            End If
        Else
            MsgBox("اختر خصم من القائمة", MsgBoxStyle.Information, "خطأ")
        End If
    End Sub
    Private Sub List_tous_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles List_tous.SelectedIndexChanged
        Try
            For Each i As Integer In List_tous.SelectedIndices
                avo = Partager.moi.List_enemi(i)
                If avo.Type = "ذاتي" Then
                    Radio_dati.Checked = True
                Else
                    Radio_ma3nawi.Checked = True
                End If
                txt_cin.Text = avo.Cin
                txt_3a2ili.Text = avo.Nom
                txt_chakhssi.Text = avo.Prenom
                txt_nom_entrep.Text = avo.Nom_ent
                txt_adress_momati9anouni.Text = avo.AddRep
                comb_ville.Text = avo.Ville
                txt_adresse.Text = avo.Adress
                txt_mail.Text = avo.Email
                txt_adresse.Text = avo.Adress
                txt_molahadat.Text = avo.Remarque
                txt_momati9anouni.Text = avo.Representant_legal

                txt_molahadat.Text = avo.Remarque
                avo.remplir_List_list_phone()

                For Each numhone As Phone In avo.list_phone
                    If numhone.Type = "Tel" Then
                        Partager.list_hatif.Add(numhone)


                    ElseIf numhone.Type = "Fix" Then
                        Partager.list_fix.Add(numhone)


                    ElseIf numhone.Type = "Fax" Then
                        Partager.list_fax.Add(numhone)
                    End If
                Next
            Next
            If List_tous.SelectedIndices.Count <= 0 Then
                Partager.vider(Me)
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub Radio_ma3nawi_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Radio_ma3nawi.CheckedChanged
        txt_momati9anouni.Enabled = True
        txt_nom_entrep.Enabled = True
        txt_adress_momati9anouni.Enabled = True

        txt_chakhssi.Enabled = False
        txt_3a2ili.Enabled = False
        typeEnemi = "معنوي"

        txt_chakhssi.Clear()
        txt_3a2ili.Clear()
    End Sub

    Private Sub Radio_dati_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Radio_dati.CheckedChanged
        txt_momati9anouni.Enabled = False
        txt_nom_entrep.Enabled = False
        txt_adress_momati9anouni.Enabled = False

        txt_momati9anouni.Clear()
        txt_nom_entrep.Clear()
        txt_adress_momati9anouni.Clear()

        txt_chakhssi.Enabled = True
        txt_3a2ili.Enabled = True
        typeEnemi = "ذاتي"
    End Sub

    Private Sub Btn_nouv_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_nouv.Click
        Partager.vider(Me)
    End Sub

    Private Sub txt_cin_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txt_cin.KeyPress
        If e.KeyChar = ChrW(32) Then
            e.Handled = True
        End If
    End Sub

    Private Sub txt_mail_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles txt_mail.Click
        If bol = False Then
            txt_mail.Text = ""
        End If
        bol = True
    End Sub

    Private Sub txt_mail_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txt_mail.KeyPress
        If e.KeyChar = ChrW(32) Then
            e.Handled = True
        End If
    End Sub

    Private Sub txt_mail_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txt_mail.TextChanged
        txt_mail.ForeColor = Color.Black
        txt_mail.TextAlign = HorizontalAlignment.Left
    End Sub


    Private Sub Btn_ref_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_ref.Click
        Frm_Liaison_Ref.Show()
    End Sub
End Class